/**
 * FileName: TestAdjacencyListsGraph.c
 * ---------------------------------------------------------------------------------------------------------------------
 * 复现图17.10
 *
 *
 */
#include <stdio.h>
#include <stdlib.h>

//边相关
typedef struct {
    int v;
    int w;
}
Edge;

//顶点相关
typedef struct node* link;
struct node {
    int v;
    link next;
};

//图相关
typedef struct graph *Graph;
struct graph {
    int V;
    int E;
    link* adj;
};

//辅助函数声明
link NEW(int, link);
Edge EDGE(int, int);
void GRAPHshow(Graph);

//图操作声明
Graph GRAPHinit(int);
void GRAPHinsertE(Graph, Edge);
void GRAPHremoveE(Graph, Edge);
int GRAPHedges(Edge [], Graph);
Graph GRAPHcopy(Graph);
void GRAPHdestroy(Graph);

//辅助函数实现
link NEW(int v, link next) {
    link x = (link) malloc(sizeof(*x));
    x->v = v;
    x->next = next;
    return x;
}

Edge EDGE(int v, int w) {
    Edge edge;
    edge.v = v;
    edge.w = w;
    return edge;
}

void GRAPHshow(Graph G) {
    printf("%d vertices, %d edges\n", G->V, G->E);
    int v;
    link t;
    for (v = 0; v < G->V; v++) {
        printf("%2d:", v);
        for (t = G->adj[v]; t != NULL; t = t->next) {
            printf(" %2d", t->v);
        }
        printf("\n");
    }
}

//图操作函数实现
Graph GRAPHinit(int V) {
    int v;
    Graph G = malloc(sizeof(*G));
    G->V = V;
    G->E = 0;
    G->adj = (link*) malloc(V * sizeof(link));
    for (v = 0; v < V; v++) {
        G->adj[v] = NULL;
    }
    return G;
}

void GRAPHinsertE(Graph G, Edge e) {
    int v = e.v;
    int w = e.w;
    G->adj[v] = NEW(w, G->adj[v]);
    G->adj[w] = NEW(v, G->adj[w]);
    G->E++;
}

void GRAPHremoveE(Graph G, Edge e) {
    //TODO
}
int GRAPHedges(Edge a[], Graph G) {
    int E = 0;
    int v;
    link t;
    for (v = 0; v < G->V; v++) {
        for (t = G->adj[v]; t != NULL; t = t->next) {
            if (v < t->v) {
                a[E++] = EDGE(v, t->v);
            }
        }
    }

    return E;
}
Graph GRAPHcopy(Graph G) {
    //TODO
}
void GRAPHdestroy(Graph G) {
    //TODO
}

//测试函数声明
void test_show();

int main(int argc, char *argv[]) {
    test_show();
    return 0;
}

//测试函数实现
void test_show() {
    int V = 13;
    Graph G = GRAPHinit(V);
    Edge edge1 = EDGE(0, 5);
    Edge edge2 = EDGE(5, 4);
    Edge edge3 = EDGE(7, 8);
    Edge edge4 = EDGE(4, 3);
    Edge edge5 = EDGE(0, 2);
    Edge edge6 = EDGE(9, 11);
    Edge edge7 = EDGE(0, 1);
    Edge edge8 = EDGE(11, 12);
    Edge edge9 = EDGE(5, 3);
    Edge edge10 = EDGE(9, 12);
    Edge edge11 = EDGE(9, 10);
    Edge edge12 = EDGE(6, 4);
    Edge edge13 = EDGE(0, 6);

    GRAPHinsertE(G, edge1);
    GRAPHinsertE(G, edge2);
    GRAPHinsertE(G, edge3);
    GRAPHinsertE(G, edge4);
    GRAPHinsertE(G, edge5);
    GRAPHinsertE(G, edge6);
    GRAPHinsertE(G, edge7);
    GRAPHinsertE(G, edge8);
    GRAPHinsertE(G, edge9);
    GRAPHinsertE(G, edge10);
    GRAPHinsertE(G, edge11);
    GRAPHinsertE(G, edge12);
    GRAPHinsertE(G, edge13);

    GRAPHshow(G);
}
